package czx.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import czx.model.enums.MenuTreeType;

/**
 * 菜单表
 * 
 * @author 陈正兴
 */
@Entity
@Table(name = "tmenu")
public class Tmenu extends OperateUser implements java.io.Serializable {

	// Fields

	private static final long serialVersionUID = 1L;
	private String id;
	/**
	 * 父级菜单
	 */
	private Tmenu tmenu;
	/**
	 * 菜单名称
	 */
	private String menuname;
	/**
	 * 菜单标题
	 */
	private String menutitle;
	/**
	 * 导航地址
	 */
	private String navigateurl;
	/**
	 * 菜单图片名
	 */
	private String menuimg;
	/**
	 * 菜单类型
	 */
	private MenuTreeType menutype;
	/**
	 * 目标
	 */
	private String target;
	/**
	 * 是否允许修改
	 */
	private boolean allowedit;
	/**
	 * 是否允许删除
	 */
	private boolean allowdelete;
	/**
	 * 排序
	 */
	private Integer sortseq;
	/**
	 * 是否已经移到回收站
	 */
	private boolean deletemark;
	/**
	 * 下级菜单
	 */
	private Set<Tmenu> tmenus = new HashSet<Tmenu>(0);

	// Constructors

	/** default constructor */
	public Tmenu() {
		this.id = UUID.randomUUID().toString();
		this.menutype = MenuTreeType.NAVIGATE;
		this.menuname = "";
		this.menutitle = "";
		this.menuimg = "";
		this.navigateurl = "";
		this.target = "";
		this.sortseq = 1;
		this.allowdelete = true;
		this.allowedit = true;
		this.deletemark = false;
	}

	/** minimal constructor */
	public Tmenu(String menuid, Tuser tuserByModifyuserid, Tuser tuserByCreateuserid, String menuname, String menutitle, String navigateurl, String menuimg, MenuTreeType menutype, String target, boolean allowedit, boolean allowdelete, Integer sortseq, boolean deletemark, Date createtime, String createusername, Date modifytime, String modifyusername) {
		this.id = menuid;
		this.menuname = menuname;
		this.menutitle = menutitle;
		this.navigateurl = navigateurl;
		this.menuimg = menuimg;
		this.menutype = menutype;
		this.target = target;
		this.allowedit = allowedit;
		this.allowdelete = allowdelete;
		this.sortseq = sortseq;
		this.deletemark = deletemark;
	}

	/** full constructor */
	public Tmenu(String menuid, Tuser tuserByModifyuserid, Tmenu tmenu, Tuser tuserByCreateuserid, String menuname, String menutitle, String navigateurl, String menuimg, MenuTreeType menutype, String target, boolean allowedit, boolean allowdelete, Integer sortseq, boolean deletemark, Date createtime, String createusername, Date modifytime, String modifyusername, Set<Tmenu> tmenus) {
		this.id = menuid;
		this.tmenu = tmenu;
		this.menuname = menuname;
		this.menutitle = menutitle;
		this.navigateurl = navigateurl;
		this.menuimg = menuimg;
		this.menutype = menutype;
		this.target = target;
		this.allowedit = allowedit;
		this.allowdelete = allowdelete;
		this.sortseq = sortseq;
		this.deletemark = deletemark;
		this.tmenus = tmenus;
	}

	// Property accessors
	@Id
	@Column(name = "MENUID", unique = true, nullable = false, length = 36)
	public String getMenuid() {
		return this.id;
	}

	public void setMenuid(String menuid) {
		this.id = menuid;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PARENTID")
	public Tmenu getTmenu() {
		return this.tmenu;
	}

	public void setTmenu(Tmenu tmenu) {
		this.tmenu = tmenu;
	}

	@Column(name = "MENUNAME", nullable = false, length = 100)
	public String getMenuname() {
		return this.menuname;
	}

	public void setMenuname(String menuname) {
		this.menuname = menuname;
	}

	@Column(name = "MENUTITLE", nullable = false, length = 100)
	public String getMenutitle() {
		return this.menutitle;
	}

	public void setMenutitle(String menutitle) {
		this.menutitle = menutitle;
	}

	@Column(name = "NAVIGATEURL", nullable = false)
	public String getNavigateurl() {
		return this.navigateurl;
	}

	public void setNavigateurl(String navigateurl) {
		this.navigateurl = navigateurl;
	}

	@Column(name = "MENUIMG", nullable = false, length = 100)
	public String getMenuimg() {
		return this.menuimg;
	}

	public void setMenuimg(String menuimg) {
		this.menuimg = menuimg;
	}

	@Column(name = "MENUTYPE", nullable = false)
	public MenuTreeType getMenutype() {
		return this.menutype;
	}

	public void setMenutype(MenuTreeType mtype) {
		this.menutype = mtype;
	}

	@Column(name = "TARGET", nullable = false, length = 50)
	public String getTarget() {
		return this.target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	@Column(name = "ALLOWEDIT", nullable = false)
	public boolean getAllowedit() {
		return this.allowedit;
	}

	public void setAllowedit(boolean allowedit) {
		this.allowedit = allowedit;
	}

	@Column(name = "ALLOWDELETE", nullable = false)
	public boolean getAllowdelete() {
		return this.allowdelete;
	}

	public void setAllowdelete(boolean allowdelete) {
		this.allowdelete = allowdelete;
	}

	@Column(name = "SORTSEQ", nullable = false)
	public Integer getSortseq() {
		return this.sortseq;
	}

	public void setSortseq(Integer sortseq) {
		this.sortseq = sortseq;
	}

	@Column(name = "DELETEMARK", nullable = false)
	public boolean getDeletemark() {
		return this.deletemark;
	}

	public void setDeletemark(boolean deletemark) {
		this.deletemark = deletemark;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "tmenu")
	public Set<Tmenu> getTmenus() {
		return this.tmenus;
	}

	public void setTmenus(Set<Tmenu> tmenus) {
		this.tmenus = tmenus;
	}

	@Override
	public void Validate() throws ValidateException {
		super.Validate();
		
		String rootMenuId = "0";
		if (this.menutype == MenuTreeType.ROOT) {
			if (this.tmenu != null)
				throw new ValidateException("根节点没有上级菜单");
			if (!this.id.equals(rootMenuId))
				throw new ValidateException("菜单表只允许一个根节点,且它的ID必须为：" + rootMenuId);
		} else {
			if (this.tmenu == null)
				throw new ValidateException(this.menutype.getName() + "的上级菜单不能为空！");
		}
	}
}